//
//  changePayWordViewController.m
//  dongfengTest
//
//  Created by wxt on 2021/3/18.
//

#import "ChangePayWordViewController.h"
#import "NewPayPasswordViewController.h"
#import "ForgetPayWordViewController.h"
@interface ChangePayWordViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)TKLtextFiled * payWordF;
@property(nonatomic,strong)UIButton * nextBtn;
@property(nonatomic,strong)UIButton * isHiddenBtn;

@end

@implementation ChangePayWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Wcolor;
   
    
    UIButton * backBtn =[UIButton createBtnFrame:CGRectMake(15, kStatusBarHeight+5, 18, 18) title:@"" bgImageName:@"fanhui" action:^(UIButton *button) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [self.view addSubview:backBtn];
    TKLlable * navTit = [TKLlable labelWithFrame:CGRectMake(kWidth/2-60, kStatusBarHeight+5, 120, 20) text:@"" textColor:Bcolor font:Font_6(16) textAlignment:Textcenter backgroundColor:Ccolor];
    [self.view addSubview:navTit];

        navTit.text= @"修改支付密码";

    
    [self addUIView];
    // Do any additional setup after loading the view.
}

-(void)addUIView{
    
    TKLlable * phoneNumLab = [TKLlable labelWithFrame:CGRectMake(20, kTopHeight+50, kWidth-40, 20) text:@"请输入当前支付密码" textColor:Bcolor font:Font_6(18) textAlignment:Textleft backgroundColor:Ccolor];
    [self.view addSubview:phoneNumLab];
    
    self.payWordF = [TKLtextFiled FiledWithFrame:CGRectMake(20, kTopHeight+90, kWidth-120, 40) text:@"  请输入支付密码" textColor:Bcolor font:Font_6(14) textAlignment:Textleft backgroundColor:Ccolor];
//    self.payWordF.clearButtonMode=UITextFieldViewModeNever;
    self.payWordF.secureTextEntry = YES;
    [self.payWordF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.payWordF];
    
    //密文切换
    
    self.isHiddenBtn = [UIButton createBtnFrame:CGRectMake(kWidth-60, kTopHeight+100, 30, 20) title:@"" bgImageName:@"yincang" action:^(UIButton *button) {

        [self ishiddenAction];
        
    }];
    self.isHiddenBtn.selected = YES;
    [self.isHiddenBtn setTitleColor:Bcolor forState:UIControlStateNormal];
    [self.view addSubview:self.isHiddenBtn];
    
    
    
    
    UIView * linView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.payWordF.frame)+5, kWidth-40, 1)];
    linView.backgroundColor = JhColor(204, 204, 204);
    [self.view addSubview:linView];
    
    
    self.nextBtn  = [UIButton createBtnFrame:CGRectMake(20, CGRectGetMaxY(linView.frame)+20, kWidth-40, 40) title:@"下一步" bgImageName:@"" action:^(UIButton *button) {
        [self.view endEditing:YES];
        self.payWordF.text = [self.payWordF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        if (self.payWordF.text.length == 6) {
            //校验PIN
            [self verifyPINAction];
        }else{
            [MBProgressHUD showError:@"请输入6位数支付密码" toView:self.view];
        }
       
        
     
    }];
    self.nextBtn.backgroundColor = JhColor(75, 149, 251);
    self.nextBtn.layer.masksToBounds = YES;
    self.nextBtn.layer.cornerRadius = 20;
    [self.view addSubview:self.nextBtn];
    
    
    UIButton * forgetBtn = [UIButton createBtnFrame:CGRectMake(kWidth/2-50, CGRectGetMaxY(self.nextBtn.frame)+10, 100, 20) title:@"忘记支付密码 >" bgImageName:@"" action:^(UIButton *button) {
        ForgetPayWordViewController * forgetVC = [[ForgetPayWordViewController alloc]init];
        forgetVC.pushPath = @"payWord";
        forgetVC.modalPresentationStyle = 0;
        [self presentViewController:forgetVC animated:YES completion:nil];
     
    }];
    [forgetBtn setTitleColor:JhColor(153, 153, 153) forState:UIControlStateNormal];
    [self.view addSubview:forgetBtn];
    
  
    
    
}
//监听长度
- (void)textFieldDidChange:(UITextField *)textField{
   
    if (textField == self.payWordF) {
        if (textField.text.length > 6) {
            self.payWordF.text = [textField.text substringToIndex:6];
//            self.nextBtn.backgroundColor = JhColor(102, 102, 102);
        }else if (textField.text.length == 6){
//            self.nextBtn.backgroundColor = JhColor(102, 102, 102);
        }else{
            
//            self.nextBtn.backgroundColor = JhColor(204, 204, 204);
        }
        
        
    }

}
-(void)ishiddenAction{
    // 取反
    if (self.isHiddenBtn.selected == NO) {
        self.isHiddenBtn.selected = YES;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"xianshi"] forState:UIControlStateNormal];
    }else{
        self.isHiddenBtn.selected = NO;
        [self.isHiddenBtn setBackgroundImage:[UIImage imageNamed:@"yincang"] forState:UIControlStateNormal];
    }
    self.payWordF.secureTextEntry = !self.payWordF.secureTextEntry;
 
// 获得输入的值

    NSString *content = self.payWordF.text;
   
 // 解决输入框从密文切换明文时出现空格的问题

    self.payWordF.text = @"";
 

    self.payWordF.text = content;
 
 //如果是密文转明文进行二次编辑不会出现清空现象,如果是明文转密文进行二次编辑就会出现清空现象,进行判断,如果切换后此时输入框是密文状态,就保存原有内容并赋值给输入框,这样就不会出现清空现象了

   if (self.payWordF.secureTextEntry) {

 // 解决输入框从明文切换为密文时进行二次编辑出现清空现象

       [self.payWordF insertText:content];
  

 }
 
    
    
}
-(void)verifyPINAction{
    
    NSUserDefaults * stand =[NSUserDefaults standardUserDefaults];
    
    IMUser *user = [IMUser userWithUserName:[stand objectForKey:@"userName"]];

    if (user == nil) {
        [MBProgressHUD  showMessage:@"用户不存在" toView:self.view];
    }else{
        [user.cert verifyPIN:self.payWordF.text andCompleteBlock:^(int ret, int remaintimes) {
            if (ret == IM_ER_SUCCESS) {
                NewPayPasswordViewController * newVC = [[NewPayPasswordViewController alloc]init];
                newVC.oldPayWord = self.payWordF.text;
                newVC.user = user;
                [self.navigationController pushViewController:newVC animated:YES];
              
            }else{
                NSLog(@"error:%@",[IMError getMsgWithErr:ret]);
                if (remaintimes > 0) {
    //                NSLog(@"PIN码校验失败,剩余%d次尝试次数",remaintimes);
                   
                    
                    [MBProgressHUD showMessage:[NSString stringWithFormat:@"支付密码校验失败,剩余%d次尝试次数",remaintimes] toView:self.view];
        
                }else{
                    [MBProgressHUD showMessage:@"账户锁定，请联系管理员" toView:self.view];
                    
                }
            }
        }];
        
    }
    
   
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
